import { createStore } from "vuex";
import {getWebSiteInfo,queryEnumType} from "@/api/common"
import user from "./modules/user"

const EnumTypeMap = {
  "tag-type":"tagEnumTypeList",
  "cate-type":"cateEnumTypeList",
  "talk-type":"talkEnumTypeList",
}
export default createStore({
  state: {
    webInfo:JSON.parse(localStorage.getItem("webInfo"))||{},
    tagEnumTypeList: JSON.parse(localStorage.getItem("tagEnumTypeList"))|| null,
    cateEnumTypeList:JSON.parse(localStorage.getItem("cateEnumTypeList"))|| null,
    talkEnumTypeList:JSON.parse(localStorage.getItem("talkEnumTypeList"))|| null
  },
  mutations: {
    GET_WEB_INFO(state,data) {
        state.webInfo = data.data;
        localStorage.setItem("webInfo",JSON.stringify(data.data));
    },
    STORE_ENUM_TYPE(state,params) {
      let key = EnumTypeMap[params.type];
      console.log(key,"key")

      state[key] = params.data;
      localStorage.setItem(key,JSON.stringify(params.data));
    }
  },
  actions: {
    async getWebInfo({commit},value) {
        const res = await getWebSiteInfo();
        commit("GET_WEB_INFO",res);
    },
    async getEnumType({commit,state},type) {
      if(state[EnumTypeMap[type]]) {
        return state[EnumTypeMap[type]];
      }
      const res = await queryEnumType({type});
      console.log(res,"enumtype")
      await commit("STORE_ENUM_TYPE",{data:res.data,type});
      return res.data;
    }
  },
  modules: {
    user
  },
});
